package tr.gen._3bit.java._2k1s.dao.model;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

import com.opensymphony.xwork2.validator.annotations.IntRangeFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RegexFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.StringLengthFieldValidator;
import com.opensymphony.xwork2.validator.annotations.Validation;

@Entity
@Validation
public class SerialInterface extends Model{

	@Id
	@GeneratedValue
	private Integer id;
	private String port;
	private Integer baudrate;
	private Integer databits;
	private Integer stopbits;
	private Integer parity;
	private String type;
	private Boolean active;
	@OneToMany(targetEntity=Node.class,mappedBy="serialInterface",fetch=FetchType.EAGER)
	private List<Node> nodes;
	@OneToMany(targetEntity=Measurement.class,mappedBy="serialInterface",fetch=FetchType.LAZY)
	private List<Measurement> measurements; 
	
	public List<Measurement> getMeasurements() {
		return measurements;
	}
	public void setMeasurements(List<Measurement> measurements) {
		this.measurements = measurements;
	}
	public List<Node> getNodes() {
		return nodes;
	}
	public void setNodes(List<Node> nodes) {
		this.nodes = nodes;
	}
	@IntRangeFieldValidator(message = "", key = "serialInterface.model.error.id.int", min = "0")
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	@RequiredStringValidator(message = "", key = "serialInterface.model.error.port.required" )
	public String getPort() {
		return port;
	}
	public void setPort(String port) {
		this.port = port;
	}
	@RequiredStringValidator(message = "", key = "serialInterface.model.error.baudrate.required" )
	@IntRangeFieldValidator(message = "", key = "serialInterface.model.error.baudrate.int", min = "300", max = "230400")
	public Integer getBaudrate() {
		return baudrate;
	}
	public void setBaudrate(Integer baudrate) {
		this.baudrate = baudrate;
	}
	@IntRangeFieldValidator(message = "", key = "serialInterface.model.error.databits.int", min = "0", max = "100")
	@RequiredStringValidator(message = "", key = "serialInterface.model.error.databits.required" )
	public Integer getDatabits() {
		return databits;
	}
	public void setDatabits(Integer databits) {
		this.databits = databits;
	}
	@IntRangeFieldValidator(message = "", key = "serialInterface.model.error.stopbits.int", min = "0", max = "100")
	@RequiredStringValidator(message = "", key = "serialInterface.model.error.stopbits.required" )
	public Integer getStopbits() {
		return stopbits;
	}
	public void setStopbits(Integer stopbits) {
		this.stopbits = stopbits;
	}
	@IntRangeFieldValidator(message = "", key = "serialInterface.model.error.parity.int", min = "0", max = "1")
	@RequiredStringValidator(message = "", key = "serialInterface.model.error.parity.required" )
	public Integer getParity() {
		return parity;
	}
	public void setParity(Integer parity) {
		this.parity = parity;
	}
	@RequiredStringValidator(message = "", key = "serialInterface.model.error.type.required" )
	@StringLengthFieldValidator(message = "", key = "serialInterface.model.error.type.length", minLength = "1", maxLength = "8")
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	@RequiredStringValidator(message = "", key = "serialInterface.model.error.active.required" )
	@RegexFieldValidator(message = "", key = "serialInterface.model.error.active.regex", expression = "(true|false)")
	public Boolean getActive() {
		return active;
	}
	public void setActive(Boolean active) {
		this.active = active;
	}
	@Transient
	public String getLabel() {
		return getId() + ": " + getPort();
	}
	@Override
	public boolean equals(Object obj) {
		if (obj instanceof SerialInterface &&
				getId() != null)
			return getId().equals(((SerialInterface)obj).getId());
		return false;
	}
	
}
